﻿#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

#region

using System;
using System.Web.UI;
using AzureMRCore.DataModel;

#endregion

namespace AzureMRUI
{
    public partial class JobData : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["JobID"] != "" && Request.QueryString["JobID"] != null)
            {
                string jobId = Request.QueryString["JobID"];
                Label2.Text = jobId;

                JobDataSource dataSource = new JobDataSource();
                int totalMap, pendingMap, executingMap, failedMap, finishedMap;
                JobDataModel jobData;
                double finishedPercentage = dataSource.GetFinishedMapPercentage(jobId, out totalMap, out finishedMap,
                                                                                out pendingMap, out executingMap,
                                                                                out failedMap, out jobData);
                if (jobData != null)
                {
                    MapExecutingLabel.Text = executingMap.ToString();
                    MapPendingLabel.Text = pendingMap.ToString();
                    MapTotalLabel.Text = "<a href='/MapTasks.aspx?JobID=" + jobId + "&refresh=15'>" +
                                         totalMap.ToString() + "</a>";
                    Chart2.ChartAreas[0].AxisX.Maximum = totalMap;
                    MapFinishedLabel.Text = finishedMap.ToString();
                    MapPercentageComplete.Text = finishedPercentage.ToString() + "%";

                    OutContainerLabel.Text = jobData.OutDir;
                    StartTimeLabel.Text = jobData.StartTime;
                    IterationLabel.Text = (jobData.CurrentIteration + 1).ToString();
                    ProParamsLabel.Text = jobData.ProgramParams;

                    if (jobData.NumReduceTasks != 0)
                    {
                        int totalRed, pendingRed, executingRed, failedRed, finishedRed;
                        double redPercentage = dataSource.GetFinishedReducePercentage(jobId, out totalRed,
                                                                                      out finishedRed, out pendingRed,
                                                                                      out executingRed, out failedRed);
                        ReduceExecutingLabel.Text = executingRed.ToString();
                        ReducePendingLabel.Text = pendingRed.ToString();
                        ReduceTotalLabel.Text = "<a href='/ReduceTasks.aspx?JobID=" + jobId + "&refresh=15'>" +
                                                totalRed.ToString() +
                                                "</a>";
                        ReduceFinishedLabel.Text = finishedRed.ToString();
                        ReducePercentageComplete0.Text = redPercentage.ToString() + "%";
                    }
                }
            }
            else
            {
                Response.Redirect("~/");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
        }

        protected double GetMapPercentage()
        {
            return 10;
        }
    }
}